<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="./layui/css/layui.css" type="text/css" />
    <link rel="stylesheet" href="./css/login.css" type="text/css" />
</head>
<body>
    <div class="login layui-anim layui-anim-up">
        <div class="login-title">管理员登录</div>
        <form  method="post" class="layui-form" action="" lay-filter="formTest">
            <input type="text" name="login_name" value="root" required  lay-verify="required" placeholder="用户名" autocomplete="off" class="layui-input">
            <input type="password" name="login_pwd" value="root" required lay-verify="required" placeholder="密码" autocomplete="off" class="layui-input">
            <input type="submit" value="登录" class="layui-btn" lay-submit lay-filter="login" />
        </form>
    </div>
</body>
</html>
<script src="./layui/layui.js"></script>
<script src="./js/ajax.js"></script>
<script>
    //Demo
    layui.use('form', function(){
        var form = layui.form;
        
        
        //监听提交
        form.on('submit(login)', function(data){
           // layer.msg(JSON.stringify(data.field));
            //var data1 = form.val("formTest");

            // 加载动画
            var index2 = layer.load(2, {time: 10*1000});
            ajax({
                url: './php/login.php',
                data: data.field,
                method: 'post',
                success: function(res){
                    // 关闭加载动画
                    layer.close(index2);
                    if(res == 1){
                        layer.alert('登录成功', function(index){
                            location.href="./index.html";
                            layer.close(index);
                        });   
                    }else if(res == 0){
                        layer.alert('用户名不存在'); 
                        form.val('formTest', {login_name: ''});
                    }else if(res == 2){
                        layer.alert('密码错误'); 
                        form.val('formTest', {login_pwd: ''});
                    }
                    else{
                        layer.alert('验证失败,请联系相关人员处理'); 
                        form.val('formTest', {login_name: '', login_pwd: ''});
                    }
                }
            });
            //console.log(data1);
            //location.href="./index.html";
            return false;
        });
    });
</script>